<?php
class Industries extends UaModel {
	
	var $name = "Industry";
	var $subHeader = "<table summary=\"子分类\" cellSpacing=5 cellPadding=5 width=\"100%\" align=center
				border=0>
						<tbody>
						  <tr>
						  <!--一列子分类 -->
							<td vAlign=top width=\"51%\">
							<table cellSpacing=3 cellPadding=1 width=\"100%\" border=0>
								<tbody>
								";
	var $subFooter = "</tbody>
							  </table></td>
						  </tr>
						</tbody>
					  </table>";
	
	function Industries() {
	
	}
	
	function getIndustries($limit_b = 0, $limit_e = 1, $conditions = null) {
		$sql = "SELECT id AS ID,name AS Name,parentid AS ParentID ";
		$sql .= "FROM " . $this->getTable ( true ) . " ";
		$sql .= "WHERE 1 " . $conditions;
		if (! is_null ( $limit_b ) && ! is_null ( $limit_e ))
			$sql .= " limit $limit_b,$limit_e";
		$res = $GLOBALS ['g_db']->GetAll ( $sql );
		return $res;
	}
	
	function getAllIndustry($conditions = null, $limit = null, $offset = null) {
		$sql = "SELECT Industry.id AS ID,name AS Name,parentid AS ParentID,product_amount AS ProductAmount,ia AS IndustryIa,ib AS IndustryIb,sell_amount AS SellAmount,buy_amount AS BuyAmount,company_amount AS CompanyAmount,product_amount,sell_amount,buy_amount,company_amount ";
		$sql .= "FROM " . $this->getTable ( true ) . " ";
		$sql .= "WHERE 1 ";
		$sql .= $conditions;
		$sql .= " ORDER BY Industry.id ";
		if (! is_null ( $limit ) && ! is_null ( $offset ))
			$sql .= " LIMIT $limit,$offset";
		$tmp_arr = $GLOBALS ['g_db']->GetAll ( $sql );
		return $tmp_arr;
	}
	
	function getIndustyName($id) {
		$id = intval ( $id );
		$sql = "SELECT name AS Name FROM " . $this->getTable ( true ) . " WHERE id=" . $id;
		$indname = $GLOBALS ['g_db']->GetOne ( $sql );
		if (empty ( $indname ) || ! $indname) {
			return false;
		} else {
			return $indname;
		}
	}
	
	function formatSubIndusty($industries, $li = null) {
		global $media_paths;
		if (is_null ( $li ))
			$li = $GLOBALS ['li'];
		global $nav;
		$return = null;
		
		if (empty ( $industries )) {
			$return = "<div id='divNextTwenty' align='center' class='time'>目前没有任何子分类。</div>";
		} elseif (is_array ( $industries )) {
			/**
			 * 对得到的子行业进行格式化，方便xajax输出
			 */
			$i = 0;
			foreach ( $industries as $inds ) {
				$return .= "<tr >
									<td height=25 align=left class=\"corhui\"><img src=\"" . $media_paths ['media_path'] . "images/icon_4.gif\" width=\"3\" height=\"5\">
									  &nbsp;<a
							href=\"" . URL . strtolower ( $nav [$li] ['ename'] ) . "/list.php?sid=" . $inds ['ID'] . "\" class=\"link_black12\">" . $inds ['Name'] . "</A><span
							class=time>(" . number_format ( $inds [strtolower ( $nav [$li] ['ename'] ) . "_amount"] ) . ")</span> </TD>
							</TR>";
				if ($i == 10)
					$return .= "</tbody>
							  </table></td>
							<td valign=top width=\"49%\"> <table cellspacing=3 cellpadding=1 width=\"100%\" border=0>
								<tbody>";
				$i ++;
				if ($i == 21) {
					$return .= "<tr >
									<td height=25 align=center class=\"corhui\"><span
							class=time><a href=\"#\" onclick=\"xajax.call('nextIndustry',[2]);\">下20个行业</span></a></td>
							</tr>";
					break;
				}
			}
		} else {
			$return = "<font color=red>数据格式错误</font>";
		}
		
		return $return;
	}
	
	/**
	 * 搜索上层以及上上层是否有parent
	 *
	 * @param 本行业id $id
	 */
	function searchParentIndustry($id) {
		$parents = null;
		$have_parent = $GLOBALS ['g_db']->GetOne ( "SELECT parentid FROM " . $this->getTable ( true ) . " WHERE Industry.id=" . $id );
		if ($have_parent) {
			
			$parents = $this->searchParentIndustry ( $have_parent );
		}
		$parents [] = $id;
		return $parents;
	}
}
?>